home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / emacssrc.zip / EMACSSRC.TAR / emacs-19.17 / src / filelock.c < prev    next >
C/C++ Source or Header  |  1993-10-07  |  12KB  |  475 lines

  1. /* Copyright (C) 1985, 1986, 1987, 1993 Free Software Foundation, Inc.
  2.  
  3. This file is part of GNU Emacs.
  4.  
  5. GNU Emacs is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. GNU Emacs is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU Emacs; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19.  
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #include "config.h"
  23.  
  24. #ifdef VMS
  25. #include "vms-pwd.h"
  26. #else
  27. #ifndef WINDOWSNT
  28. #include <pwd.h>
  29. #endif /* !WINDOWSNT */
  30. #endif
  31.  
  32. #include <errno.h>
  33. #ifndef WINDOWSNT
  34. #include <sys/file.h>
  35. #endif /* !WINDOWSNT */
  36. #ifdef USG
  37. #include <fcntl.h>
  38. #endif /* USG */
  39.  
  40. #include "lisp.h"
  41. #include "paths.h"
  42. #include "buffer.h"
  43.  
  44. extern int errno;
  45.  
  46. #ifndef USE_PROTOTYPES
  47. extern char *egetenv ();
  48. extern char *strcpy ();
  49. #endif
  50.  
  51. #ifdef CLASH_DETECTION
  52.   
  53. /* If system does not have symbolic links, it does not have lstat.
  54.    In that case, use ordinary stat instead.  */
  55.  
  56. #ifndef S_IFLNK
  57. #define lstat stat
  58. #endif
  59.  
  60.  
  61. /* The name of the directory in which we keep lock files, with a '/'
  62.    appended.  */  
  63. char *lock_path;
  64.  
  65. /* The name of the file in the lock directory which is used to
  66.    arbitrate access to the entire directory.  */
  67. #define SUPERLOCK_NAME "!!!SuperLock!!!"
  68.  
  69. /* The path to the superlock file.  This is SUPERLOCK_NAME appended to
  70.    lock_path.  */
  71. char *superlock_path;
  72.  
  73. /* Set LOCK to the name of the lock file for the filename FILE.
  74.    char *LOCK; Lisp_Object FILE;  */
  75.  
  76. #ifndef HAVE_LONG_FILE_NAMES
  77.  
  78. #define MAKE_LOCK_PATH(lock, file) \
  79.   (lock = (char *) alloca (14 + strlen (lock_path) + 1), \
  80.    fill_in_lock_short_file_name (lock, (file)))
  81.  
  82.  
  83. fill_in_lock_short_file_name (lockfile, fn)
  84.      register char *lockfile;
  85.      register Lisp_Object fn;
  86. {
  87.   register union
  88.     {
  89.       unsigned int  word [2];
  90.       unsigned char byte [8];
  91.     } crc;
  92.   register unsigned char *p, new;
  93.   
  94.   /* 7-bytes cyclic code for burst correction on byte-by-byte basis.
  95.      the used polynomial is D^7 + D^6 + D^3 +1. pot@cnuce.cnr.it */
  96.  
  97.   crc.word[0] = crc.word[1] = 0;
  98.  
  99.   for (p = XSTRING (fn)->data; new = *p++; )
  100.     {
  101.       new += crc.byte[7];
  102.       crc.byte[7] = crc.byte[6];
  103.       crc.byte[6] = crc.byte[5] + new;
  104.       crc.byte[5] = crc.byte[4];
  105.       crc.byte[4] = crc.byte[3];
  106.       crc.byte[3] = crc.byte[2] + new;
  107.       crc.byte[2] = crc.byte[1];
  108.       crc.byte[1] = crc.byte[0];
  109.       crc.byte[0] = new;
  110.     }
  111.   sprintf (lockfile, "%s%.2x%.2x%.2x%.2x%.2x%.2x%.2x", lock_path,
  112.        crc.byte[0], crc.byte[1], crc.byte[2], crc.byte[3],
  113.        crc.byte[4], crc.byte[5], crc.byte[6]);
  114. }
  115.  
  116. #else /* defined HAVE_LONG_FILE_NAMES */
  117.  
  118. #define MAKE_LOCK_PATH(lock, file) \
  119.   (lock = (char *) alloca (XSTRING (file)->size + strlen (lock_path) + 1), \
  120.    fill_in_lock_file_name (lock, (file)))
  121.  
  122.  
  123. fill_in_lock_file_name (lockfile, fn)
  124.      register char *lockfile;
  125.      register Lisp_Object fn;
  126. {
  127.   register char *p;
  128.  
  129.   strcpy (lockfile, lock_path);
  130.  
  131.   p = lockfile + strlen (lockfile);
  132.  
  133.   strcpy (p, XSTRING (fn)->data);
  134.  
  135.   for (; *p; p++)
  136.     {
  137.       if (*p == '/')
  138.     *p = '!';
  139.     }
  140. }
  141. #endif /* !defined HAVE_LONG_FILE_NAMES */
  142.  
  143. static Lisp_Object
  144. lock_file_owner_name (lfname)
  145.      char *lfname;
  146. {
  147.   struct stat s;
  148.   struct passwd *the_pw;
  149. #ifdef WINDOWSNT
  150.   /*
  151.    * FIXME!  what's the analog?  If we want to lock files we're editing, we
  152.    * should use the file-system to do it, not a hack.
  153.    */
  154.  
  155.   DEBUGPRINT("EMACS broken @ "__FILE__ ": %d\n", __LINE__);
  156. #else /* !WINDOWSNT */
  157.   extern struct passwd *getpwuid ();
  158.  
  159.   if (lstat (lfname, &s) == 0)
  160.     the_pw = getpwuid (s.st_uid);
  161. #endif /* !WINDOWSNT */
  162.   return (the_pw == 0 ? Qnil : build_string (the_pw->pw_name));
  163. }
  164.  
  165.  
  166. /* lock_file locks file fn,
  167.    meaning it serves notice on the world that you intend to edit that file.
  168.    This should be done only when about to modify a file-visiting
  169.    buffer previously unmodified.
  170.    Do not (normally) call lock_buffer for a buffer already modified,
  171.    as either the file is already locked, or the user has already
  172.    decided to go ahead without locking.
  173.  
  174.    When lock_buffer returns, either the lock is locked for us,
  175.    or the user has said to go ahead without locking.
  176.  
  177.    If the file is locked by someone else, lock_buffer calls
  178.    ask-user-about-lock (a Lisp function) with two arguments,
  179.    the file name and the name of the user who did the locking.
  180.    This function can signal an error, or return t meaning
  181.    take away the lock, or return nil meaning ignore the lock.  */
  182.  
  183. /* The lock file name is the file name with "/" replaced by "!"
  184.    and put in the Emacs lock directory.  */
  185. /* (ie., /ka/king/junk.tex -> /!/!ka!king!junk.tex). */
  186.  
  187. /* If HAVE_LONG_FILE_NAMES is not defined, the lock file name is the hex
  188.    representation of a 14-bytes CRC generated from the file name
  189.    and put in the Emacs lock directory (not very nice, but it works).
  190.    (ie., /ka/king/junk.tex -> /!/ec92d3ed24a8f0). */
  191.  
  192. void
  193. lock_file (fn)
  194.      register Lisp_Object fn;
  195. {
  196.   register Lisp_Object attack;
  197.   register char *lfname;
  198.  
  199.   MAKE_LOCK_PATH (lfname, fn);
  200.  
  201.   /* See if this file is visited and has changed on disk since it was
  202.      visited.  */
  203.   {
  204.     register Lisp_Object subject_buf = Fget_file_buffer (fn);
  205.     if (!NILP (subject_buf)
  206.     && NILP (Fverify_visited_file_modtime (subject_buf))
  207.     && !NILP (Ffile_exists_p (fn)))
  208.       call1 (intern ("ask-user-about-supersession-threat"), fn);
  209.   }
  210.  
  211.   /* Try to lock the lock. */
  212.   if (lock_if_free (lfname) <= 0)
  213.     /* Return now if we have locked it, or if lock dir does not exist */
  214.     return;
  215.  
  216.   /* Else consider breaking the lock */
  217.   attack = call2 (intern ("ask-user-about-lock"), fn,
  218.           lock_file_owner_name (lfname));
  219.   if (!NILP (attack))
  220.     /* User says take the lock */
  221.     {
  222.       lock_superlock (lfname);
  223.       lock_file_1 (lfname, O_WRONLY) ;
  224.       unlink (superlock_path);
  225.       return;
  226.     }
  227.   /* User says ignore the lock */
  228. }
  229.  
  230. /* Lock the lock file named LFNAME.
  231.    If MODE is O_WRONLY, we do so even if it is already locked.
  232.    If MODE is O_WRONLY | O_EXCL | O_CREAT, we do so only if it is free.
  233.    Return 1 if successful, 0 if not.  */
  234.  
  235. int
  236. lock_file_1 (lfname, mode)
  237.      int mode; char *lfname; 
  238. {
  239.   register int fd;
  240.   char buf[20];
  241.  
  242.   if ((fd = open (lfname, mode, 0666)) >= 0)
  243.     {
  244. #ifdef USG
  245.       chmod (lfname, 0666);
  246. #else
  247.       fchmod (fd, 0666);
  248. #endif
  249.       sprintf (buf, "%d ", getpid ());
  250.       write (fd, buf, strlen (buf));
  251.       close (fd);
  252.       return 1;
  253.     }
  254.   else
  255.     return 0;
  256. }
  257.  
  258. /* Lock the lock named LFNAME if possible.
  259.    Return 0 in that case.
  260.    Return positive if lock is really locked by someone else.
  261.    Return -1 if cannot lock for any other reason.  */
  262.  
  263. int
  264. lock_if_free (lfname)
  265.      register char *lfname; 
  266. {
  267.   register int clasher;
  268.  
  269.   while (lock_file_1 (lfname, O_WRONLY | O_EXCL | O_CREAT) == 0)
  270.     {
  271.       if (errno != EEXIST)
  272.     return -1;
  273.       clasher = current_lock_owner (lfname);
  274.       if (clasher != 0)
  275.     if (clasher != getpid ())
  276.       return (clasher);
  277.     else return (0);
  278.       /* Try again to lock it */
  279.     }
  280.   return 0;
  281. }
  282.  
  283. /* Return the pid of the process that claims to own the lock file LFNAME,
  284.    or 0 if nobody does or the lock is obsolete,
  285.    or -1 if something is wrong with the locking mechanism.  */
  286.  
  287. int
  288. current_lock_owner (lfname)
  289.      char *lfname;
  290. {
  291.   int owner = current_lock_owner_1 (lfname);
  292.   if (owner == 0 && errno == ENOENT)
  293.     return (0);
  294.   /* Is it locked by a process that exists?  */
  295. #ifdef WINDOWSNT
  296.   DEBUGPRINT("EMACS broken @ "__FILE__ ": %d\n", __LINE__);
  297. #else /* !WINDOWSNT */
  298.   if (owner != 0 && (kill (owner, 0) >= 0 || errno == EPERM))
  299. #endif /* !WINDOWSNT */
  300.     return (owner);
  301.   if (unlink (lfname) < 0)
  302.     return (-1);
  303.   return (0);
  304. }
  305.  
  306. int
  307. current_lock_owner_1 (lfname)
  308.      char *lfname;
  309. {
  310.   register int fd;
  311.   char buf[20];
  312.   int tem;
  313.  
  314.   fd = open (lfname, O_RDONLY, 0666);
  315.   if (fd < 0)
  316.     return 0;
  317.   tem = read (fd, buf, sizeof buf);
  318.   close (fd);
  319.   return (tem <= 0 ? 0 : atoi (buf));
  320. }
  321.  
  322.  
  323. void
  324. unlock_file (fn)
  325.      register Lisp_Object fn;
  326. {
  327.   register char *lfname;
  328.  
  329.   MAKE_LOCK_PATH (lfname, fn);
  330.  
  331.   lock_superlock (lfname);
  332.  
  333.   if (current_lock_owner_1 (lfname) == getpid ())
  334.     unlink (lfname);
  335.  
  336.   unlink (superlock_path);
  337. }
  338.  
  339. lock_superlock (lfname)
  340.      char *lfname;
  341. {
  342.   register int i, fd;
  343.  
  344.   for (i = -20; i < 0 && (fd = open (superlock_path,
  345.                      O_WRONLY | O_EXCL | O_CREAT, 0666)) < 0;
  346.        i++)
  347.     {
  348.       if (errno != EEXIST)
  349.     return;
  350.       sleep (1);
  351.     }
  352.   if (fd >= 0)
  353.     {
  354. #ifdef USG
  355.       chmod (superlock_path, 0666);
  356. #else
  357.       fchmod (fd, 0666);
  358. #endif
  359.       write (fd, lfname, strlen (lfname));
  360.       close (fd);
  361.     }
  362. }
  363.  
  364. void
  365. unlock_all_files ()
  366. {
  367.   register Lisp_Object tail;
  368.   register struct buffer *b;
  369.  
  370.   for (tail = Vbuffer_alist; XGCTYPE (tail) == Lisp_Cons;
  371.        tail = XCONS (tail)->cdr)
  372.     {
  373.       b = XBUFFER (XCONS (XCONS (tail)->car)->cdr);
  374.       if (XTYPE (b->filename) == Lisp_String &&
  375.       b->save_modified < BUF_MODIFF (b))
  376.     unlock_file (b->filename);
  377.     }
  378. }
  379.  
  380.  
  381. DEFUN ("lock-buffer", Flock_buffer, Slock_buffer,
  382.   0, 1, 0,
  383.   "Lock FILE, if current buffer is modified.\n\
  384. FILE defaults to current buffer's visited file,\n\
  385. or else nothing is done if current buffer isn't visiting a file.")
  386.   (fn)
  387.      Lisp_Object fn;
  388. {
  389.   if (NILP (fn))
  390.     fn = current_buffer->filename;
  391.   else
  392.     CHECK_STRING (fn, 0);
  393.   if (current_buffer->save_modified < MODIFF
  394.       && !NILP (fn))
  395.     lock_file (fn);
  396.   return Qnil;    
  397. }
  398.  
  399. DEFUN ("unlock-buffer", Funlock_buffer, Sunlock_buffer,
  400.   0, 0, 0,
  401.  "Unlock the file visited in the current buffer,\n\
  402. if it should normally be locked.")
  403.   ()
  404. {
  405.   if (current_buffer->save_modified < MODIFF &&
  406.       XTYPE (current_buffer->filename) == Lisp_String)
  407.     unlock_file (current_buffer->filename);
  408.   return Qnil;
  409. }
  410.  
  411.  
  412. /* Unlock the file visited in buffer BUFFER.  */
  413.  
  414. unlock_buffer (buffer)
  415.      struct buffer *buffer;
  416. {
  417.   if (buffer->save_modified < BUF_MODIFF (buffer) &&
  418.       XTYPE (buffer->filename) == Lisp_String)
  419.     unlock_file (buffer->filename);
  420. }
  421.  
  422. DEFUN ("file-locked-p", Ffile_locked_p, Sfile_locked_p, 0, 1, 0,
  423.   "Return nil if the FILENAME is not locked,\n\
  424. t if it is locked by you, else a string of the name of the locker.")
  425.   (fn)
  426.   Lisp_Object fn;
  427. {
  428.   register char *lfname;
  429.   int owner;
  430.  
  431.   fn = Fexpand_file_name (fn, Qnil);
  432.  
  433.   MAKE_LOCK_PATH (lfname, fn);
  434.  
  435.   owner = current_lock_owner (lfname);
  436.   if (owner <= 0)
  437.     return (Qnil);
  438.   else if (owner == getpid ())
  439.     return (Qt);
  440.   
  441.   return (lock_file_owner_name (lfname));
  442. }
  443.  
  444.  
  445. /* Initialization functions.  */
  446.  
  447. init_filelock ()
  448. {
  449.   lock_path = egetenv ("EMACSLOCKDIR");
  450.   if (! lock_path)
  451.     lock_path = PATH_LOCK;
  452.  
  453.   /* Make sure it ends with a slash.  */
  454.   if (lock_path[strlen (lock_path) - 1] != '/')
  455.     {
  456.       lock_path = strcpy ((char *) xmalloc (strlen (lock_path) + 2),
  457.               lock_path);
  458.       strcat (lock_path, "/");
  459.     }
  460.  
  461.   superlock_path = (char *) xmalloc ((strlen (lock_path)
  462.                       + sizeof (SUPERLOCK_NAME)));
  463.   strcpy (superlock_path, lock_path);
  464.   strcat (superlock_path, SUPERLOCK_NAME);
  465. }
  466.  
  467. syms_of_filelock ()
  468. {
  469.   defsubr (&Sunlock_buffer);
  470.   defsubr (&Slock_buffer);
  471.   defsubr (&Sfile_locked_p);
  472. }
  473.  
  474. #endif /* CLASH_DETECTION */
  475.